package service

import (
	"github.com/toolkits/pkg/logger"
	coreV1 "k8s.io/api/core/v1"
	"strings"
)

type CreateDeploymentRequest struct {
	NamespaceName       string               `json:"namespace_name"`
	DeploymentName      string               `json:"deployment_name"`
	Image               string               `json:"image"`
	PortsNum            []int32              `json:"ports_num"`
	Replicas            int32                `json:"replicas"`
	MountConfigmapInfos []MountConfigmapInfo `json:"mount_configmap_infos"`
	MountPvcInfos       []MountPvcInfo       `json:"mount_pvc_infos"`
	HostPathInfos       []HostPathInfo       `json:"host_path_infos"`
	ImagePullSecrets    []string             `json:"image_pull_secrets"`
}

type ApplyDeploymentRequest struct {
	NamespaceName    string            `json:"namespace_name"`
	DeploymentName   string            `json:"deployment_name"`
	Image            string            `json:"image"`
	Replicas         int32             `json:"replicas"`
	Labels           map[string]string `json:"labels"`
	ImagePullSecrets []string          `json:"image_pull_secrets"`
}

type ApplyDeploymentByImageRequest struct {
	NamespaceName  string `json:"namespace_name"`
	DeploymentName string `json:"deployment_name"`
	Image          string `json:"image"`
}

type RollBackDeploymentRequest struct {
	NamespaceName  string `json:"namespace_name"`
	DeploymentName string `json:"deployment_name"`
	ReplicasetName string `json:"replicaset_name"`
}

type DeleteDeploymentRequest struct {
	NamespaceName  string `json:"namespace_name"`
	DeploymentName string `json:"deployment_name"`
}

type MountForDeploymentRequest struct {
	NamespaceName       string               `json:"namespace_name"`
	DeploymentName      string               `json:"deployment_name"`
	MountConfigmapInfos []MountConfigmapInfo `json:"mount_configmap_infos"`
	MountPvcInfos       []MountPvcInfo       `json:"mount_pvc_infos"`
	HostPathInfos       []HostPathInfo       `json:"host_path_infos"`
}

type MountConfigmapInfo struct {
	FileName      string `json:"file_name"`
	MountPath     string `json:"mount_path"`
	ConfigmapName string `json:"configmap_name"`
}

type MountPvcInfo struct {
	MountPath string `json:"mount_path"`
	PvcName   string `json:"pvc_name"`
}

type HostPathInfo struct {
	MountPathIn  string `json:"mount_path_in"`
	MountPathOut string `json:"mount_path_out"`
	Tag          string `json:"tag"`
}

func explainConfigmap(mountConfigmapInfos []MountConfigmapInfo) (volumeMounts []coreV1.VolumeMount, volumes []coreV1.Volume) {
	for _, mountInfo := range mountConfigmapInfos {
		//拼接 volumeMount.Name
		volumeMountNameSuffix := strings.Split(mountInfo.FileName, ".")[0]
		volumeMountName := mountInfo.ConfigmapName + volumeMountNameSuffix
		logger.Info(volumeMountName)
		//给volumeMount赋值
		volumeMount := coreV1.VolumeMount{
			Name:      volumeMountName,
			MountPath: mountInfo.MountPath + "/" + mountInfo.FileName,
			SubPath:   mountInfo.FileName,
		}
		volumeMounts = append(volumeMounts, volumeMount)

		//给volume赋值
		volume := coreV1.Volume{
			Name: volumeMountName,
			VolumeSource: coreV1.VolumeSource{
				ConfigMap: &coreV1.ConfigMapVolumeSource{
					LocalObjectReference: coreV1.LocalObjectReference{
						Name: mountInfo.ConfigmapName,
					},
				},
			},
		}
		volumes = append(volumes, volume)
	}
	return volumeMounts, volumes
}

func explainPvc(mountPvcInfos []MountPvcInfo) (volumeMounts []coreV1.VolumeMount, volumes []coreV1.Volume) {
	for _, mountInfo := range mountPvcInfos {
		//拼接 volumeMount.Name
		volumeMountName := mountInfo.PvcName
		logger.Info(volumeMountName)
		//给volumeMount赋值
		volumeMount := coreV1.VolumeMount{
			Name:      volumeMountName,
			MountPath: mountInfo.MountPath,
		}
		volumeMounts = append(volumeMounts, volumeMount)

		//给volume赋值
		volume := coreV1.Volume{
			Name: volumeMountName,
			VolumeSource: coreV1.VolumeSource{
				PersistentVolumeClaim: &coreV1.PersistentVolumeClaimVolumeSource{
					ClaimName: mountInfo.PvcName,
				},
			},
		}
		volumes = append(volumes, volume)
	}
	return volumeMounts, volumes
}

func explainHostPath(hostPathInfo []HostPathInfo) (volumeMounts []coreV1.VolumeMount, volumes []coreV1.Volume) {
	for _, mountInfo := range hostPathInfo {
		//拼接 volumeMount.Name
		volumeMountName := mountInfo.Tag
		logger.Info(volumeMountName)
		//给volumeMount赋值
		volumeMount := coreV1.VolumeMount{
			Name:      volumeMountName,
			MountPath: mountInfo.MountPathIn,
		}
		volumeMounts = append(volumeMounts, volumeMount)

		//给volume赋值
		volume := coreV1.Volume{
			Name: volumeMountName,
			VolumeSource: coreV1.VolumeSource{
				HostPath: &coreV1.HostPathVolumeSource{
					Path: mountInfo.MountPathOut,
				},
			},
		}
		volumes = append(volumes, volume)
	}
	return volumeMounts, volumes
}
